<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Session;
use DB;
use Crypt;

class UserController extends Controller
{
    //
    public function userInfo()
    {
        if(session('user_info'))
        {  
            $data = DB::table('users')
              -> select('users.*','users_info.pic','users_info.signature','users_info.jifen','users_info.qdtimes')
              -> leftJoin('users_info','users.id','=','users_info.user_id')
              -> where('users.id','=',Session('user_info')->id) 
              -> first();
            // dd($data);
            return view('home.user.userinfo', ['data' => $data,'title' => '个人中心']);


        }else
        {
            return view('home.user.login',['title'=>'用户登录']);
        }
        
    }
    // public function userinfo($id)
    // {

      
    //     dd($data);
    //   return view('home.user.userinfo',['title' => '用户编辑', ]);
      
    // }
    
    public function info_edit(Request $request)
    {   
        $this -> validate($request,[
            'name' => 'min:6|max:18',
            'email' => 'email',
            'phone' => 'digits:11',
            'pic' => 'image|max:2048',
            ],[
            'name.min' => '用户名最少6位',
            'name.max' => '用户名最多18位',
            'email.email' => '请输入正确的邮箱格式',
            'phone.digits' => '请输入11位数字是手机号',
            'pic.image' => '你必须传一张图片',
            'pic.max' => '最大2M' 
            ]);
        $id = $request -> id;
        // $pic = $request -> file('pic');
        $data = $request -> except('_token','id','pic','signature');
        // dd($data);
        // dd($request);
        
         $ispic = '';
         if($request -> hasFile('pic'))
         {
            // dd(111);
            if($request -> file('pic') -> isValid())
            {
              //处理头像
              //获取文件后缀
              $extension = $request -> file('pic') -> getClientOriginalExtension();
              // dd($extension);
              $filename = mt_rand(100000,999999999).'.'.$extension;

              $dir = './uploads/avatar/';

              $request -> file('pic') -> move($dir, $filename);

              $userInfoData['pic'] = '/uploads/avatar/'.$filename;
              // dd($userInfoData['pic']);
              //获取原来的图片
              $oldPic = DB::table('users_info') -> where('user_id',$id) -> first() -> pic;

              //判断是不是默认值
              if(!empty($oldPic))
              {
                  if($oldPic != '/uploads/avatar/default.jpg')
                 {
                      if(file_exists('.'.$oldPic))
                      {
                        unlink('.'.$oldPic);
                      }
                    
                  }
              }

              $ispic = DB::table('users_info') -> where('user_id',$id) -> update($userInfoData);
            
            }
          
         }
         $dataInfo['signature'] = $request -> signature;
         // dd($data);
         // dd($signature);
         $news = DB::table('users_info') -> where('user_id',$id) -> update($dataInfo);
         $res = DB::table('users') -> where('id',$id) -> update($data);
         // dd($res);
         if($res && $news)
         {
          return redirect('/home/user/userinfo') -> with(['info' => '修改成功']);
         }else
         {
          return back() -> with(['info' => '修改失败']);
         }
    }


    public function editpassword()
    {

         return view('home.user.editpassword',['title' =>'修改密码']);
    }

    public function update(Request $request)
    {
        $password = $request -> password;
        $newpas = $request -> newpassword;
        $renewpas = $request -> renewpassword;
        $id  = Session('user_info')->id;
        // dd($id);
        $upwd = DB::table('users') -> where('id',$id) -> first() -> password;

        if($password == Crypt::decryptString($upwd))
        {
             if($newpas == $renewpas)
             {
                   $data['password'] = Crypt::encryptString($newpas);
                   $res = DB::table('users') -> where('id',$id) -> update($data);
                   if($res)
                   {
                        return redirect('home/user/userinfo') -> with(['info' => '修改成功']);
                   }else
                   {
                        return back() -> with(['info' => '修改失败']);
                   }
             }else
             {
                return back() -> with(['info' => '两次密码不一致,重新输入']);
             }
        }else
        {
            return back() -> with(['info' => '原密码错误,重新输入']);
        }


    }


    public function userorder()
    {
    	if(session('user_info'))
    	{
    		$data = DB::table('order') -> where('uid', Session('user_info')->id) -> orderby('id','desc') -> get();
    		// dd($data);
    		$detail = [];
    		foreach($data as $row)
    		{
    			$detail[$row -> id] = DB::table('detail') -> where('oid', $row -> id) -> get();
    			$detail[$row -> id] -> num = 0;
    			foreach($detail[$row -> id] as $line)
    			{	
    				$detail[$row -> id] ->num += ($line -> price) * ($line -> count);
    				$line -> gid = DB::table('goods') -> where('gid', $line -> gid) -> first();
    			}
    		}
    		// dd($detail);
    		return view('home.user.userorder',['title'=>'我的订单','data' => $data, 'detail' => $detail]);

    	}else
    	{
    		return view('home.user.login',['title'=>'用户登录']);
    	}
    }

    public function usersc()
    {
        if(session('user_info'))
        {   
            $data = DB::table('shoucang') -> where('uid',Session('user_info')->id) -> get();
            
            foreach($data as $row)
            {
               $row -> gid = DB::table('goods') -> where('gid', $row -> gid) -> first();
            }
            // dd($data);
            return view('home.user.shoucang',['title'=>'我的收藏', 'data' => $data]);

        }else
        {
            return view('home.user.login',['title'=>'用户登录']);
        }
    }

    public function delsc($id)
    {
        $res = DB::table('shoucang') -> where([['gid','=',$id],['uid','=',Session('user_info') -> id]]) -> delete();
        if($res)
        {
            return back();
        }else
        {
            return back();
        }
    }

    public function qd(Request $request)
    {
        $oldtimes = $request -> oldtimes;
        if($oldtimes == 0)
        {   
            $jifen = DB::table('users_info') -> where('user_id', Session('user_info') -> id) ->first() -> jifen;
            $data['lqdtime'] = Date('Ymd');
            
            $data['qdtimes'] = 1;
            $data['jifen'] = $jifen + 10;
            $res = DB::table('users_info') -> where('user_id', Session('user_info') -> id) -> update($data);
            if($res)
            {
              return $data;

            }else{
                $data['shibai'] = '签到失败';
                return $data;
            }
            // return $data;
        }else
        {
            $shuju = DB::table('users_info') -> where('user_id', Session('user_info') -> id) ->first();
            $data['lqdtime'] = Date('Ymd');

            if($data['lqdtime'] == $shuju -> lqdtime)
            {   
                $data['shibai'] = '今天您已签到';
                return $data;
            }else
            {   
                // return 111 ;
                $data['qdtimes'] = $shuju -> qdtimes + 1 ;
                $data['jifen'] = $shuju -> jifen + 10;
                $res = DB::table('users_info') -> where('user_id', Session('user_info') -> id) -> update($data);
                if($res)
                {
                  return $data;

                }else{
                    $data['shibai'] = '签到失败';
                    return $data;
                }
            }
        }
         
    }

     public function cj()
    {
        // return 111;
        $jifen = DB::table('users_info') -> where('user_id', Session('user_info') -> id) ->first() -> jifen;
        if($jifen >= 88)
        {   
            // return 111;
            $data['jifen'] = $jifen - 88;
            $res = DB::table('users_info') -> where('user_id', Session('user_info') -> id) -> update($data);

            if($res){
                $a=rand(1,100);
                if($a == 2 ){
                    $data['jiangpin'] = '恭喜您中了一等奖:欧洲10国2日游';
                    return $data;
                }elseif($a>10&&$a<21){
                    $data['jiangpin'] ='恭喜您中了二等奖:东莞半日游(路费自付)';
                    return $data;
                }else{
                    $data['jiangpin'] ='恭喜您中了三等奖:兄弟连任意游';
                    return $data;
                }
            }else{
                return '抽奖失败';
            }
        }else
        {
          $data['shibai'] = '您的积分不足';
          return $data;
        }
    }
      
}
